namespace Vanilla.Tools.TextEditor

import Vanilla
import Vanilla.UI

class PlainTextGadget (Gadget):
	Text as string:
		get:
			t = ""
			for i in Lines:
				t += i
				t += "\n"
			return t
		set:
			Lines = value.Split(char('\n'))
	
	Lines as (string) = array(string, 0)
	
	FirstVisibleLine as int = 0
	VisibleLines as int = 10
	
	Font as Font = Font()
	
	MinSize as Size:
		get:
			return Size(100,100)
	
	def constructor (p as Box):
		super(p)
	
	override def Draw (b as Brush):
		b.Draw(Rect(Size))
		b.Colour = Colour.White.Alpha(0.5)
		b.FillPreserve()
		b.Colour = Colour.Grey
		b.Stroke()
		
		DrawContents(b)
	
	override def DrawContents (b as Brush):
		b.Font = Font
		b.Colour = Colour.Black
		b.LineWidth = 2
		b.MoveTo(50,50)
		
		for i in Lines: //[FirstVisibleLine:FirstVisibleLine+VisibleLines]:
			print "write ${i}"
			b.Write(i)
			//b.Stroke()
			b.RelMoveTo(0,20)

class TextEditorWindow (MainWindow):
	File as File

	def constructor (f as File):
		if f is null:
			super("Text Editor")
		else:
			super("${f.Name} - Text Editor")
		
		File = f
		ch = PlainTextGadget(self)
		
		ch.Text = "Whooooop\nChicken?\nPotata's!!!"
	
class TextEditorTool (UI.Tool, IOpen, IOpenFile):
	def Open ():
		TextEditorWindow(null)
	
	def OpenFile (f as File):
		TextEditorWindow(f)
